package com.yztc.orangeentertainment.ui.main.subscribefragment.staritem;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.StarViewPagerAdapter;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.bean.staritembean.StarInfoStarBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.ui.login.LoginActivity;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.starfeedfragment.FeedFragment;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.starnewsfragment.NewsFrament;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.starpicturefragment.PictureFragment;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.starwishfragment.WishFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/7.
 */
public class StarItemActivity extends BaseActivity implements StarItemContract.View, View.OnClickListener {
    @InjectView(R.id.iv_staritem_bg)
    ImageView ivStaritemBg;
    @InjectView(R.id.tv_staritem_name)
    TextView tvStaritemName;
    @InjectView(R.id.tv_staritem_week)
    TextView tvStaritemWeek;
    @InjectView(R.id.tv_staritem_sub)
    TextView tvStaritemSub;
    @InjectView(R.id.tv_title)
    TextView title;
    @InjectView(R.id.tv_staritem_con)
    TextView tvStaritemCon;
    @InjectView(R.id.iv_staritem_pop)
    ImageView ivStaritemPop;
    @InjectView(R.id.tabs)
    TabLayout mTablayout;
    @InjectView(R.id.appbar)
    AppBarLayout appbar;
    @InjectView(R.id.vp_module)
    ViewPager vpModule;
    @InjectView(R.id.toolBar)
    Toolbar mToolBar;
    @InjectView(R.id.rel_staritem)
    RelativeLayout rel;
    @InjectView(R.id.collapsing)
    CollapsingToolbarLayout collapsing;
    @InjectView(R.id.layout_staritem)
    LinearLayout layout;
    //popuwindow view
    private TextView starpopName;
    private TextView starpopBir;
    private TextView starpopHeight;
    private TextView starpopWeight;
    private TextView starpopBlood;
    private TextView starpopCountry;
    private TextView starpopStar;
    private TextView starpopLead;

    private StarItemPresenter presenter;
    private String sid;
    private List<String> list = new ArrayList<>();
    private List<Fragment> fragments;
    private NewsFrament newsFragment;
    private PictureFragment pictureFragment;
    private FeedFragment feedFragment;
    private WishFragment wishFragment;
    private PopupWindow popupwindow;
    private StarInfoStarBean starInfo;
    private boolean flag;
    private String id;
    public static final int REQUEST_CODE = 1;
    @Override
    public int getLayoutId() {
        return R.layout.activity_staritem;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
        intiShow();
        initPresenter();
        initViewPager();
    }

    private void initViewPager() {
        fragments = new ArrayList<>();

        Bundle bundle = new Bundle();
        bundle.putString("sid", sid);
        //资讯
        newsFragment = new NewsFrament();
        newsFragment.setArguments(bundle);
        //图集
        pictureFragment = new PictureFragment();
        pictureFragment.setArguments(bundle);
        //留言板
        wishFragment = new WishFragment();
        wishFragment.setArguments(bundle);
        //动态
        feedFragment = new FeedFragment();
        feedFragment.setArguments(bundle);

        fragments.add(newsFragment);
        fragments.add(pictureFragment);
        fragments.add(wishFragment);
        fragments.add(feedFragment);
        //viewpager adapter
        vpModule.setAdapter(new StarViewPagerAdapter(getSupportFragmentManager(), fragments));
        //tablaout初始化
        vpModule.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTablayout));
        mTablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vpModule.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initPresenter() {
        presenter = new StarItemPresenter(this);
        Map<String, String> map = new HashMap<>();
        //参数map
        map.put(SubscribeUrlConfig.Params.SID, sid);
        map.put(SubscribeUrlConfig.Params.MAC, SubscribeUrlConfig.All_DefaultVaule.MAC_VALUE);
        map.put(SubscribeUrlConfig.Params.UID, SubscribeUrlConfig.All_DefaultVaule.UID_VALUE);
        map.put(SubscribeUrlConfig.Params.PF, SubscribeUrlConfig.All_DefaultVaule.PF_VALUE);
        map.put(SubscribeUrlConfig.Params.NET, SubscribeUrlConfig.All_DefaultVaule.NET_VALUE);
        map.put(SubscribeUrlConfig.Params.ACCESSTOKEN, SubscribeUrlConfig.All_DefaultVaule.ACCESSTOKEN_VALUE);
        map.put(SubscribeUrlConfig.Params.CHANNEL, SubscribeUrlConfig.All_DefaultVaule.CHANNEL_VALUE);
        map.put(SubscribeUrlConfig.Params.VER, SubscribeUrlConfig.All_DefaultVaule.VER_VALUE);
        map.put(SubscribeUrlConfig.Params.RES, SubscribeUrlConfig.All_DefaultVaule.RES_VALUE);
        presenter.getStarItemFromNet(map, this);
    }

    private void intiShow() {
        sid = getIntent().getStringExtra("sid");
        ivStaritemPop.setOnClickListener(this);
        tvStaritemSub.setOnClickListener(this);
    }

    /**
     * 获取starbean类对象
     * @param starBean
     */
    @Override
    public void onLoadStarItemSuccess(final StarInfoStarBean starBean) {
        starInfo = starBean;
        //添加数据
        tvStaritemName.setText(starBean.getName());
        tvStaritemWeek.setText(getResources().getString(R.string.week) + starBean.getRank());
        tvStaritemCon.setText(starBean.getDescription());
        Glide.with(this).load(starBean.getBgpic()).into(ivStaritemBg);
        //设置遮罩层
        ivStaritemBg.setColorFilter(0x40000000);
        list = starBean.getModule();
        for (int i = 0; i < list.size(); i++) {
            mTablayout.addTab(mTablayout.newTab().setText(list.get(i)));
        }
        //向上滑动事件
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    //展开状态
                    rel.setVisibility(View.VISIBLE);
                    title.setVisibility(View.GONE);
                } else if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                    //折叠状态
                    rel.setVisibility(View.GONE);
                    title.setVisibility(View.VISIBLE);
                    title.setText(starBean.getName());
                } else {
                    //中间状态
                    rel.setVisibility(View.GONE);
                    title.setVisibility(View.VISIBLE);
                    title.setText(starBean.getName());
                }
            }
        });
        setSupportActionBar(mToolBar);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    /**
     * 初始化popupwindow数据
     */
    private void initPopupWindowData() {
        starpopName.setText(starInfo.getName());
        starpopBir.setText(getResources().getString(R.string.bir)+"  "+ starInfo.getBir());
        starpopHeight.setText(getResources().getString(R.string.height)+"  " + starInfo.getHeight());
        starpopWeight.setText(getResources().getString(R.string.weight)+"  " + starInfo.getWeight());
        starpopBlood.setText(getResources().getString(R.string.blood)+"  " + starInfo.getBloodtype());
        starpopCountry.setText(getResources().getString(R.string.country)+"  "+ starInfo.getCountry());
        starpopStar.setText(getResources().getString(R.string.star)+"  "+ starInfo.getStarsign());
        starpopLead.setText(getResources().getString(R.string.renwu) +"  "+ starInfo.getShareTxtlead());
    }

    @Override
    public void onLoadStarItemFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_staritem_pop:
                // popupwindow 点击事件
                if (popupwindow != null && popupwindow.isShowing()) {
                    popupwindow.dismiss();
                    return;
                } else {
                    initmPopupWindowView();
                    initPopupWindowData();
                    popupwindow.showAtLocation(layout, Gravity.FILL,0,0);
                }
                break;
            case R.id.tv_staritem_sub:
                //订阅按钮点击事件
                if (!flag){
                    tvStaritemSub.setText(R.string.sub);
                    tvStaritemSub.setTextColor(Color.WHITE);
                    tvStaritemSub.setBackgroundResource(R.mipmap.bg_comment_switch);
                    flag=true;
                }else {
                    tvStaritemSub.setText(R.string.subed);
                    tvStaritemSub.setTextColor(Color.WHITE);
                    tvStaritemSub.setBackgroundColor(getResources().getColor(R.color.orange));
                    flag=false;
                }
                break;

        }
    }

    /**
     * 初始化 popupwindow
     */
    private void initmPopupWindowView() {
        // // 获取自定义布局文件pop.xml的视图
        View customView = getLayoutInflater().inflate(R.layout.star_item_popupwindow,
                null, false);
        popupwindow = new PopupWindow(customView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        // 自定义view添加触摸事件
        popupwindow.setBackgroundDrawable(new BitmapDrawable());
        popupwindow.setAnimationStyle(R.style.AnimationFade);
        popupwindow.setFocusable(true);
        popupwindow.setOutsideTouchable(true);
        customView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupwindow != null && popupwindow.isShowing()) {
                    popupwindow.dismiss();
                }
                return true;
            }
        });
        //popupwindow里面的数据
        starpopName = (TextView) customView.findViewById(R.id.starpop_name);
        starpopBir = (TextView) customView.findViewById(R.id.starpop_bir);
        starpopHeight = (TextView) customView.findViewById(R.id.starpop_height);
        starpopWeight = (TextView) customView.findViewById(R.id.starpop_weight);
        starpopBlood = (TextView) customView.findViewById(R.id.starpop_blood);
        starpopCountry = (TextView) customView.findViewById(R.id.starpop_country);
        starpopStar = (TextView) customView.findViewById(R.id.starpop_star);
        starpopLead = (TextView) customView.findViewById(R.id.starpop_lead);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //通过请求码和结果码判断为本次请求
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            id = data.getStringExtra("id");
        }
    }
}
